<?php
namespace App\Services;
use Illuminate\Support\Facades\Redis;
use GuzzleHttp\Client;
use App\Services\WorkService;
use Log;

class WorkkfService
{
    // 添加客服帐号
    public static function kfAccountAdd($corp_id,$name,$media_id)
    {
        $access_token = WorkService::getCropAccessToken($corp_id);
        $client = new Client();
        $response = $client->request("POST", "https://qyapi.weixin.qq.com/cgi-bin/kf/account/add?access_token=".$access_token, ["json" => ["name"=>$name,"media_id"=>$media_id]],["timeout"=>3]);
        $body = $response->getBody()->getContents();
        return json_decode($body);
    }

    // 删除客服帐号
    public static function kfAccountDel($corp_id,$open_kfid)
    {
        $access_token = WorkService::getCropAccessToken($corp_id);
        $client = new Client();
        $response = $client->request("POST", "https://qyapi.weixin.qq.com/cgi-bin/kf/account/del?access_token=".$access_token, ["json" => ["open_kfid"=>$open_kfid]],["timeout"=>3]);
        $body = $response->getBody()->getContents();
        return json_decode($body);
    }

    // 修改客服帐号
    public static function kfAccountUpdate($corp_id,$name,$media_id,$open_kfid)
    {
        $access_token = WorkService::getCropAccessToken($corp_id);
        $client = new Client();
        $response = $client->request("POST", "https://qyapi.weixin.qq.com/cgi-bin/kf/account/update?access_token=".$access_token, ["json" => ["open_kfid"=>$open_kfid,"name"=>$name,"media_id"=>$media_id]],["timeout"=>3]);
        $body = $response->getBody()->getContents();
        return json_decode($body);
    }

    // 获取客服帐号列表
    public static function kfAccountList($corp_id,$offset)
    {
        $access_token = WorkService::getCropAccessToken($corp_id);
        $client = new Client();
        $response = $client->request("POST", "https://qyapi.weixin.qq.com/cgi-bin/kf/account/list?access_token=".$access_token, ["json" => ["offset"=>$offset,"limit"=>100]],["timeout"=>3]);
        $body = $response->getBody()->getContents();
        return json_decode($body);
    }

    // 获取客服帐号链接
    public static function kfAddContactWay($corp_id,$open_kfid,$scene)
    {
        $access_token = WorkService::getCropAccessToken($corp_id);
        $client = new Client();
        $response = $client->request("POST", "https://qyapi.weixin.qq.com/cgi-bin/kf/add_contact_way?access_token=".$access_token, ["json" => ["open_kfid"=>$open_kfid,"scene"=>$scene]],["timeout"=>3]);
        $body = $response->getBody()->getContents();
        return json_decode($body);
    }

    // 添加接待人员
    public static function kfServicerAdd($corp_id,$open_kfid,$userid_list,$department_id_list)
    {
        $access_token = WorkService::getCropAccessToken($corp_id);
        $client = new Client();
        $response = $client->request("POST", "https://qyapi.weixin.qq.com/cgi-bin/kf/servicer/add?access_token=".$access_token, ["json" => ["open_kfid"=>$open_kfid,"userid_list"=>$userid_list,"department_id_list"=>$department_id_list]],["timeout"=>3]);
        $body = $response->getBody()->getContents();
        return json_decode($body);
    }

    // 删除接待人员
    public static function kfServicerDel($corp_id,$open_kfid,$userid_list,$department_id_list)
    {
        $access_token = WorkService::getCropAccessToken($corp_id);
        $client = new Client();
        $response = $client->request("POST", "https://qyapi.weixin.qq.com/cgi-bin/kf/servicer/del?access_token=".$access_token, ["json" => ["open_kfid"=>$open_kfid,"userid_list"=>$userid_list,"department_id_list"=>$department_id_list]],["timeout"=>3]);
        $body = $response->getBody()->getContents();
        return json_decode($body);
    }

    // 获取接待人员列表
    public static function kfServicerList($corp_id,$open_kfid)
    {
        $access_token = WorkService::getCropAccessToken($corp_id);
        $client = new Client();
        $response = $client->request("GET", "https://qyapi.weixin.qq.com/cgi-bin/kf/servicer/list?access_token=".$access_token."&open_kfid=".$open_kfid,["timeout"=>3]);
        $body = $response->getBody()->getContents();
        return json_decode($body);
    }

    // 读取消息
    public static function kfSyncMsg($corp_id,$cursor,$token,$limit)
    {
        $access_token = WorkService::getCropAccessToken($corp_id);
        $client = new Client();
        $response = $client->request("POST", "https://qyapi.weixin.qq.com/cgi-bin/kf/sync_msg?access_token=".$access_token, ["json" => ["cursor"=>$cursor,"token"=>$token,"limit"=>$limit,"voice_format"=>0]],["timeout"=>3]);
        $body = $response->getBody()->getContents();
        return json_decode($body);
    }

    // 发送消息
    public static function kfSendMsg($corp_id,$params)
    {
        $access_token = WorkService::getCropAccessToken($corp_id);
        $client = new Client();
        $response = $client->request("POST", "https://qyapi.weixin.qq.com/cgi-bin/kf/send_msg?access_token=".$access_token, ["json" => $params],["timeout"=>3]);
        $body = $response->getBody()->getContents();
        return json_decode($body);
    }

    // 发送欢迎语等事件响应消息
    public static function sendMsgOnEvent($corp_id,$params)
    {
        $access_token = WorkService::getCropAccessToken($corp_id);
        $client = new Client();
        $response = $client->request("POST", "https://qyapi.weixin.qq.com/cgi-bin/kf/send_msg_on_event?access_token=".$access_token, ["json" => $params],["timeout"=>3]);
        $body = $response->getBody()->getContents();
        return json_decode($body);
    }

    // 获取会话状态
    public static function servicerStateGet($corp_id,$open_kfid,$external_userid)
    {
        $access_token = WorkService::getCropAccessToken($corp_id);
        $client = new Client();
        $response = $client->request("POST", "https://qyapi.weixin.qq.com/cgi-bin/kf/service_state/get?access_token=".$access_token, ["json" => ["open_kfid"=>$open_kfid,"external_userid"=>$external_userid]],["timeout"=>3]);
        $body = $response->getBody()->getContents();
        return json_decode($body);
    }

    // 变更会话状态
    public static function serviceStateTrans($corp_id,$open_kfid,$external_userid,$service_state,$servicer_userid)
    {
        $access_token = WorkService::getCropAccessToken($corp_id);
        $client = new Client();
        $response = $client->request("POST", "https://qyapi.weixin.qq.com/cgi-bin/kf/service_state/trans?access_token=".$access_token, ["json" => ["open_kfid"=>$open_kfid,"external_userid"=>$external_userid,"service_state"=>$service_state,"servicer_userid"=>$servicer_userid]],["timeout"=>3]);
        $body = $response->getBody()->getContents();
        return json_decode($body);
    }

    // 获取客户基础信息
    public static function kfCustomerBatchget($corp_id,$external_userid_list,$need_enter_session_context)
    {
        $access_token = WorkService::getCropAccessToken($corp_id);
        $client = new Client();
        $response = $client->request("POST", "https://qyapi.weixin.qq.com/cgi-bin/kf/customer/batchget?access_token=".$access_token, ["json" => ["external_userid_list"=>$external_userid_list,"need_enter_session_context"=>$need_enter_session_context]],["timeout"=>3]);
        $body = $response->getBody()->getContents();
        return json_decode($body);
    }

    // 获取「客户数据统计」企业汇总数据
    public static function getKfCorpStatistic($corp_id,$open_kfid,$start_time,$end_time)
    {
        $access_token = WorkService::getCropAccessToken($corp_id);
        $client = new Client();
        $response = $client->request("POST", "https://qyapi.weixin.qq.com/cgi-bin/kf/get_corp_statistic?access_token=".$access_token, ["json" => ["open_kfid"=>$open_kfid,"start_time"=>$start_time,"end_time"=>$end_time]],["timeout"=>3]);
        $body = $response->getBody()->getContents();
        return json_decode($body);
    }

    // 获取「客户数据统计」接待人员明细数据
    public static function getKfServicerStatistic($corp_id,$open_kfid,$servicer_userid,$start_time,$end_time)
    {
        $access_token = WorkService::getCropAccessToken($corp_id);
        $client = new Client();
        $response = $client->request("POST", "https://qyapi.weixin.qq.com/cgi-bin/kf/get_servicer_statistic?access_token=".$access_token, ["json" => ["open_kfid"=>$open_kfid,"servicer_userid"=>$servicer_userid,"start_time"=>$start_time,"end_time"=>$end_time]],["timeout"=>3]);
        $body = $response->getBody()->getContents();
        return json_decode($body);
    }
}
